home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
v10n07.arc
/
UNIQUE.ARC
/
UNIQUE.ASM
< prev
next >
Wrap
Assembly Source File
|
1991-03-27
|
7KB
|
218 lines
;UNIQUE.COM
;
;For use in a batch file. Replaces one of the batch file parameters (%1 - %9)
;with an 8 character unique file name (i.e. the name of a file that does not
;already exist in the current directory). Unique does not create the file, it
;just finds a name.
;
;USAGE: UNIQUE n where n is the number of the parameter to be replaced
;
;An example of a batch file that uses unique follows:
;
; ECHO OFF
; ...
; REM Some batch statements
; ...
; UNIQUE 2
; COPY OldData %2
; ...
; REM Some more batch statements
; ...
; TYPE %2
; DEL %2
;
;The command line statement calling this batch file would look something like:
;
; DOBATCH Parameter1 12345678 AnotherParameter
;
;The parameter 12345678 is a dummy parameter that will be replaced by the
;unique file name. It must be 8 characters long. When the batch file gets
;to the line 'UNIQUE 2' Unique executes and replaces the second parameter on
;the command line with the unique file name. If the second parameter does not
;exists or is not 8 characters long then Unique aborts with an error message.
;After this line has been executed %2 contains the unique name so
;'COPY OldDate %2' copys the file OldData to the unique file found by Unique.
;
;ERRORLEVELs returned are: 1 - No parameter # specified
; 2 - Parameter # specifed does not exists
; 3 - Parameter # specifed is not 8 characters long
; 4 - A unique file name could not be found
cseg segment
assume cs:cseg, ds:cseg, es:cseg
cr equ 0dh
lf equ 0ah
beep equ 7
org 100h
unique proc near
start: mov si,80h ;parse command line parameters
inc si
@@: lodsb
cmp al,0dh ;check for end of command line
jnz not_end
mov dx,offset no_num ;if at end and no number has been
mov al,1 ;found then exit with error message
jmp exit
not_end: cmp al,'1' ;is the character a number?
jl @B ;no
cmp al,'9' ;maybe
jg @B ;no
mov [param_num],al ;yes. put in error messages for later
mov [param_num2],al ;use if needed.
and al,0fh ;change ASCII code to a 0 thru 8
dec al
shl al,1 ;multiple by 2 and save
push ax
push ds
search: mov bx,ax ;search backward for COMMAND MCB
mov ax,word ptr ds:[16h] ;get pointer to owner MCB
mov ds,ax
cmp ax,word ptr ds:[16h] ;if block owns itself we have it
jnz search ;otherwise go try again
mov ds,bx
mov dx,ax
mov ax,word ptr ds:[2ch] ;get segment of the environment
dec ax ;of the first program in the
xor si,si ;chain after COMMAND
@@: dec ax
mov ds,ax ;search backwards for previous
cmp byte ptr [si],4dh ;MCB
jnz @B
cmp [si+1],dx ;check if owned by COMMAND
jnz @B ;if it is we are ready to start
pop ds
inc ax
mov es,ax ;make ES point to environment
pop ax ;retrieve parameter to be replaced
mov si,0dh ;check if it exists by get the
add si,ax ;pointer to it.
cmp word ptr es:[si],0ffffh ;if ffffh it doesn't exist
jnz @F
mov dx,offset no_param ;display error message and quit
mov al,2
jmp exit
@@: push word ptr es:[si] ;save the pointer to the param.
cld
mov di,word ptr es:[si] ;check if it is the right length
mov cx,9
mov al,cr ;if a CR (0dh) is found before 8
repnz scasb ;char. have been scanned it is
jnz @F ;too short. If the 9th char.
jcxz len_ok ;isn't a CR it is too long.
@@: mov dx,offset bad_len ;if so show error message and quit
mov al,3
jmp exit
len_ok: mov env_seg,es ;save pointer to environment
push ds
pop es
mov ah,30h ;get DOS version
int 21h
cmp al,2 ;version 2.x or greater?
jge @F
mov dx,offset bad_dos ;quit with error message if not
mov ah,9 ;DOS 2.x or later
int 21h
int 20h ;quit
@@: cmp al,3 ;DOS 3.x?
jl dos2 ;if not DOS 3.x we have to find
mov dx,offset new_name ;the unique name. if it is 3.x or
mov ah,5ah ;later we let DOS create a temp
mov cx,0 ;file for us.
int 21h
jc no_unique ;leave if it couldn't do it
mov bx,ax ;otherwise close & delete it
mov ah,3eh ;close file
int 21h
mov ah,41h ;delete file
int 21h
jmp stuff_name
dos2: mov dx,offset dos2file ;search dir. for our temp file.
mov cx,0ffffh ;check for file with any attribute.
mov ah,4eh
int 21h
jc stuff_dos2 ;if not there we have what we want.
mov si,offset dos2file ;otherwise we modify the name we
mov dx,si ;used and check again.
dec dx
add si,8
@@: dec si
cmp dx,si ;if we've set all 8 char. to z
jne keep_going ;then we're finished without a
no_unique: mov dx,offset cant_do ;unique name
mov al,4
jmp exit
keep_going: cmp byte ptr [si],'z' ;search for the last z in the temp
jz @B ;file name
inc byte ptr [si] ;once we have it bump the char.
;right after it up one letter.
@@: inc si ;change any remaining char. to a
cmp byte ptr [si],0 ;at the end of the name?
jz dos2 ;loop back if so.
mov byte ptr [si],'a'
jmp @B
stuff_dos2: mov si,offset dos2file
jmp @F
stuff_name: mov si,offset new_name+2
@@: mov ax,env_seg ;retrieve to env. segment address
mov es,ax
pop di
mov cx,8 ;move the file name into the
rep movsb ;environment
mov al,0dh ;mark the end of the parameter
stosb ;just in case
mov al,0 ;clear the ERRORLEVEL and
mov dx,0 ;the error message pointer
exit: cmp dx,0 ;check if we need to display
jz @F ;anything
push ax
mov ah,9h
int 21h
mov dx,offset help_msg ;display help
mov ah,9
int 21h
pop ax
@@: mov ah,4ch ;leave
int 21h
new_name db '.\'
db 9 dup (0)
env_seg dw 0
no_param db beep,cr,lf,'Dummy parameter %'
param_num db '0 not present on command line.',cr,lf,'$'
bad_len db beep,cr,lf,'Parameter %'
param_num2 db '0 is not 8 characters long.',cr,lf,'$'
no_num db beep,cr,lf,'No parameter number was specified.',cr,lf,'$'
bad_dos db beep,cr,lf,'DOS Version 2.0 or greater required.',cr,lf,'$'
cant_do db beep,cr,lf,"Can't create unique file.",cr,lf,'$'
dos2file db 'aaaaaaaa',0
help_msg db cr,lf
db 'UNIQUE.COM Version 1.0 by Ken Hipple (CIS: 74076,2331)',cr,lf
db 'FREE software Copyright 1989 All Right Reserved No warranty implied or given',cr,lf
db lf
db 'For use in a batch file. Replaces one of the batch file parameters (%1 - %9)',cr,lf
db 'with an 8 char. unique file name.',cr,lf
db lf
db 'USAGE: UNIQUE n where n is the number of the parameter to be replaced',cr,lf
db lf
db 'Example batch file Example command line',cr,lf
db lf
db ' ECHO OFF DOBATCH Param1 12345678 Param2',cr,lf
db ' UNIQUE 2',cr,lf
db ' COPY OldData %2',cr,lf
db ' TYPE %2',cr,lf
db ' DEL %2',cr,lf
db lf
db 'The parameter 12345678 is a dummy parameter that will be replaced by the',cr,lf
db 'unique file name. It must be 8 characters long. ERRORLEVELs returned are:',cr,lf
db '1-No parameter # specified 2-Parameter specifed does not exist',cr,lf
db '3-Parameter specifed not 8 char. long 4-A unique name could not be found',cr,lf,'$'
unique endp
cseg ends
end start